<?php

namespace paper\dao;


class Paginate
{

    private int $rows;
    private int $page;
    private int $pages;
    private int $limit;
    private Query $query;

    public function __construct(int $total, int $page, int $limit)
    {
        $this->rows = $total;
        $this->limit = $limit;

        $this->pages = max(1,ceil($this->rows / $this->limit));

        if ($page < 1)
            $this->page = 1;
        else if ($page > $this->pages)
            $this->page = $this->pages;
        else
            $this->page = $page;
    }

    public function setList(array $res)
    {
    }

    /**
     * @return int
     */
    public function total(): int
    {
        return $this->rows;
    }

    public function setQuery(Query $query)
    {
        $this->query = $query;
    }

    public function select(): array
    {
        return $this->query->limit($this->getStart(), $this->limit)->select();
    }

    public function getStart(): int
    {
        return ($this->page - 1) * $this->limit;
    }

    public function getPageNo(): int
    {
        return $this->page;
    }
}